package com.lizhaoblog.pro006scheduled.controller;

import com.lizhaoblog.pro006scheduled.annotation.RequestLimit;
import com.lizhaoblog.pro006scheduled.common.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.lizhaoblog.pro006scheduled.Redisson.APIResult;
import com.lizhaoblog.pro006scheduled.annotation.RedissonLock;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestController {
    @Autowired
    private RedisUtil redisUtil;

	@PostMapping(value = "testLock", consumes = "application/json")
    @RedissonLock(keyParts = "name,age")
    public APIResult testLock(@RequestBody JSONObject params) {
        /**
         * 分布式锁key=params.getString("name")+params.getString("age");
         * 此时name和age均相同的请求不会出现并发问题
         */
        //TODO 业务处理
        return new APIResult(0, "SUCCESS");
    }
    @RequestMapping(value = "/limit")
    @ResponseBody
    public Map<String, Object> safeLimitRes(HttpServletRequest request, Integer pagenum, Integer pagesize){
        Map<String, Object> outputMap =new HashMap<String,Object>();
        outputMap.put("time",redisUtil.getExpire("req-limit-http://localhost:8080/limitTest0:0:0:0:0:0:0:1")) ;
        outputMap.put("success", false);
        outputMap.put("msg", "HTTP请求超出设定的限制");
        return outputMap;
    }

    @RequestLimit(count=10,time=60)
    @RequestMapping(value = "/limitTest")
    @ResponseBody
    public Map<String, Object> test(HttpServletRequest request, Integer pagenum, Integer pagesize){
        Map<String, Object> outputMap =new HashMap<String,Object>();

        outputMap.put("success", true);
        outputMap.put("msg", "你好帅哥");
        return outputMap;
	}
}
